package com.mx.project.controller;

import com.mx.project.common.resp.ApiResult;
import com.mx.project.common.util.JWTService;
import com.mx.project.pojo.SLove;
import com.mx.project.vo.SLoveInfo;
import com.mx.project.services.SLoveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "收藏商品")
@RestController
@RequestMapping("slove")
public class SLoveController {

    @Autowired
    private SLoveService sLoveService;

    @Autowired
    private JWTService jwtService;

    @ApiOperation("这样请求即可   ../slove/1")
    @GetMapping("/{pid}")
    public ApiResult addSLove(@PathVariable("pid") Integer pid, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult();
        Integer uid = jwtService.getUserId(request.getHeader("token"));
        SLove sLove = sLoveService.getSLoveByUidAndPid(uid, pid);
        if(sLove == null){
            int count = sLoveService.addSLove(uid, pid);
        }
        return apiResult;
    }

    @ApiOperation("直接请求即可")
    @GetMapping("")
    public ApiResult showSLove(HttpServletRequest request) {
        ApiResult apiResult = new ApiResult();
        Integer uid = jwtService.getUserId(request.getHeader("token"));
        List<SLoveInfo> lists = sLoveService.getSLoveByUid(uid);
        apiResult.setData(lists);
        return apiResult;
    }

    @ApiOperation("直接请求即可")
    @DeleteMapping("/{pid}")
    public ApiResult delSLove(@PathVariable("pid") Integer pid, HttpServletRequest request) {
        ApiResult apiResult = new ApiResult();
        Integer uid = jwtService.getUserId(request.getHeader("token"));
        int count = sLoveService.removeSLoveByUidAndPid(uid, pid);
        return apiResult;
    }

}
